import * as addressService from '../services/address';

import dataCenter from '../utils/dataCenter';
import { Toast } from 'antd-mobile'
import { Link, browserHistory } from 'dva/router';

import { getUserId } from '../utils/utils';

export default {

  namespace: 'address',

  state: {
      province: null,
      addressList: [],
      shippingList: []
  },

  subscriptions: {
    setup({ dispatch, history }) {  // eslint-disable-line
    }
  },

  effects: {
    *fetchShipping({payload}, {call, put}) {
      const { data } = yield call(addressService.fetchShipping, payload) 

      yield put({
        type: 'saveShipping',
        payload: {
          shippingList: data && data.result || []
        }
      })
    },
    *createAddress({payload}, {call, put}) {
      const { data } = yield call(addressService.createAddress, payload) 

      if(data && data.status === 1) {
        // success
        Toast.info('新建地址成功', 1);
        // to main page
        window.setTimeout(() => {
          browserHistory.goBack()
        }, 1000)
      } else {
        // error
        Toast.info(`${data && data.msg || '新建收货地址失败'}`);
      }
    },
    *modifyAddress({payload}, {call, put}) {
      const { data } = yield call(addressService.modifyAddress, payload) 

      if(data && data.status === 1) {
        // success
        if(payload.noWarning) {
          yield put({ type: 'fetchAddress', payload: getUserId() })
        } else {
          Toast.info('修改地址成功', 1);
        }
        // to main page
        window.setTimeout(() => {
          browserHistory.push('/address')
        }, 1000)
      } else {
        // error
        Toast.info(`${data && data.msg || '修改收货地址失败'}`);
      }
    },
    *deleteAddress({ payload }, { call, put }) {
      const { data } = yield call(addressService.deleteAddress, payload) 

      if(data && data.status === 1) {
        yield put({ type: 'fetchAddress', payload: getUserId() })
        Toast.info('删除收货地址成功', 1)
      } else {
        Toast.info(`${data && data.msg || '删除收货地址失败'}`);
      }
    },
    *fetchAddress({ payload }, { call, put }) {
      const { data } = yield call(addressService.fetchAddress, payload)
      yield put({
        type: 'saveAddressList',
        payload: {
          addressList: data && data.result || []
        }
      })

    },
    *fetchRegion({ payload }, { call, put }) {
      const { data } = yield call(addressService.fetchRegion, payload.regionId)
      payload.callback && payload.callback(data && data.result || [])
    },
    *fetchProvince({ payload }, { call, put }) {
      const { data } = yield call(addressService.fetchProvince) 

      try {
        let firstProvice = data.result[0]
      
        let tfakedata = yield call(addressService.fetchRegion, firstProvice.regionId)
        let tdata = tfakedata.data.result
        tdata.map(d => {
          d.value = d.regionId
          d.label = d.regionName
        })
            
        let thfakedata = yield call(addressService.fetchRegion, tdata[0].regionId)
        let thdata = thfakedata.data.result
        thdata.map(d => {
          d.value = d.regionId
          d.label = d.regionName
        })

        tdata[0].children = thdata
        firstProvice.children = tdata
      } catch (e) {

      }

      yield put({
        type: 'saveProvince',
        payload: {
          province: data && data.result || []
        }
      })
    }
  },

  reducers: {
    saveAddressList(state, { payload: { addressList } }) {
      addressList && addressList.map(address => {
        if(address.isDefault == '1') {
          dataCenter.address = dataCenter.address ? dataCenter.address : {}
          dataCenter.address.defaultAddress = address
        }
      })
      return { ...state, addressList }
    },
    saveProvince( state, { payload: { province }}) {
      province.map(province => {
        province.label = province.regionName
        province.value = province.regionId
      })
      return { ...state, province }
    },
    saveShipping(state, { payload: { shippingList }}) {
      return { ...state, shippingList }
    }
  }

};
